package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.Category;

import java.io.ObjectOutput;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

    @Override
    public List<Category> findAll() throws SQLException {
        String sql = "SELECT * from `kt_category` where is_deleted = 0 ORDER BY parent_id,sort asc;";
        return executeQuery(sql,rs->{
            List<Category> list = new ArrayList<>();
            try {
                while (rs.next()){
                    list.add(extractCategory(rs));
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return list;
        });
    }

    public Category extractCategory(ResultSet  rs) throws SQLException {
        Category category = new Category();
        category.setId(rs.getString("id"));
        category.setName(rs.getString("name"));
        category.setParent_id(rs.getString("parent_id"));
        category.setIcon(rs.getString("icon"));
        category.setSort(rs.getInt("sort"));
        category.setStatus(rs.getInt("status"));
        category.setIs_deleted(rs.getInt("is_deleted"));
        category.setUpdate_time(rs.getString("update_time"));
        return category;
    }

    @Override
    public int update(Category entity) throws SQLException {
        StringBuilder sql = new StringBuilder("UPDATE `kt_category` SET ");
        List<Object> params = new ArrayList<>();
        if(entity.getName() != null && !entity.getName().trim().isEmpty()){
            sql.append(" name = ? ,");
            params.add(entity.getName());
        }
        if(entity.getParent_id() != null && !entity.getParent_id().trim().isEmpty()){
            sql.append(" parent_id = ? ,");
            params.add(entity.getParent_id());
        }
        if(entity.getIcon() != null && !entity.getIcon().trim().isEmpty()){
            sql.append(" icon = ? ,");
            params.add(entity.getIcon());
        }
        if(entity.getSort() != null){
            sql.append(" sort = ? ,");
            params.add(entity.getSort());
        }
        if(entity.getStatus() != null){
            sql.append(" status = ? ,");
            params.add(entity.getStatus());
        }
        sql.append(" update_time = now() where id = ?");
        params.add(entity.getId());
        return executeUpdate(sql.toString(), params.toArray());
    }

    @Override
    public int insert(Category entity) throws SQLException {
        String sql = "INSERT INTO `ktmall`.`kt_category`(`name`, `parent_id`, `icon`, `sort`, `status`) VALUES (?, ?, ?, ?, ?);";
        return executeUpdate(sql, entity.getName(), entity.getParent_id(), entity.getIcon(), entity.getSort(), entity.getStatus());
    }

    @Override
    public int delete(Long id) throws SQLException {
        String sql = "UPDATE `kt_category` SET is_deleted = 1 WHERE id = ?";
        return executeUpdate(sql, id);
    }
}
